package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCardMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    //1：添加购物车
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        //判断数据库表该商品是否已经存在
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        List<ShoppingCart> list1 = shoppingCardMapper.list(shoppingCart);
        System.out.println(list1.size()+",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,");
        if (list1 != null && list1.size()>0){
            //如果已存在，只需要将数量+1
            ShoppingCart cart = list1.get(0);
            cart.setNumber(cart.getNumber()+1);
            shoppingCardMapper.updateNumberById(cart);
        }else {
            //如果不存在，需要插入一条购物车数据

            //先判断添加到购物车的是菜品还是套餐
            Long dishId = shoppingCartDTO.getDishId();
            if (dishId != null){
                //添加的是菜品
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }else {
                //添加的是套餐
                Long setmealId = shoppingCartDTO.getSetmealId();
                Setmeal byId = setmealMapper.getById(setmealId);
                shoppingCart.setName(byId.getName());
                shoppingCart.setImage(byId.getImage());
                shoppingCart.setAmount(byId.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            //把整理好的数据插入到
            shoppingCardMapper.insert(shoppingCart);
        }

    }

    //2：查看购物车
    @Override
    public List<ShoppingCart> showShoppingCart() {
        Long userId = BaseContext.getCurrentId();
        ShoppingCart sc = new ShoppingCart();
        sc.setUserId(userId);
        List<ShoppingCart> list = shoppingCardMapper.list(sc);
        return list;
    }


    //3：清空购物车
    @Override
    public void cleanShoppingCart() {
        Long userId = BaseContext.getCurrentId();
        shoppingCardMapper.deleteByUserId(userId);
    }

    //4：删除购物车中的一个商品
    @Override
    public void deleteShoppingOne(ShoppingCartDTO shoppingCartDTO) {
        //判断菜品是否存在数据库中
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        List<ShoppingCart> list = shoppingCardMapper.list(shoppingCart);
        if (list != null && list.size()>0){
            //若数据不为1，则数量减一
            Integer number = list.get(0).getNumber();
            if(number != null && number>1){
                ShoppingCart cart = list.get(0);
                cart.setNumber(cart.getNumber()-1);
                shoppingCardMapper.updateNumberById(cart);
            }else {
                //判断菜品的数量若是为1，那就直接删除掉
                shoppingCardMapper.deleteByDish(shoppingCart);
            }
        }
    }
}
